package com.rjwh.dingdong.client.activity.community;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.aishua.lib.utils.AsStrUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.adapter.CommunityPostMyMainPageAdapter;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.TopicData;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.mvp.presenter.impl.CommunityPostMyMainPagePresenter;
import com.rjwh.dingdong.client.mvp.view.CommunityPostMyMainPageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Kay
 * @ClassName CommunityPostMyMainPageActivity
 * @Description
 * @date 2016-7-29
 * @history 2016-7-29 author: description:
 */
public class CommunityPostMyMainPageActivity extends BaseActivity implements CommunityPostMyMainPageView, AdapterView.OnItemClickListener {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.community_post_my_main_page_listview)
    ListView communityPostMyMainPageListview;
    @BindView(R.id.community_post_my_main_page_mPullRefreshView)
    SwipyRefreshLayout communityPostMyMainPageMPullRefreshView;
    private int pageNumber = 0;
    private String userid;
    private String username;

    private CommunityPostMyMainPagePresenter presenter;
    private CommunityPostMyMainPageAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.community_post_my_main_page_layout);
        ButterKnife.bind(this);
        parseIntent();
        initView();
        presenter = new CommunityPostMyMainPagePresenter(this, this);
        communityPostMyMainPageMPullRefreshView.doPullRefreshing();
    }

    private void initView() {
        initToolbarHelper();
        communityPostMyMainPageMPullRefreshView.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                switch (direction) {
                    case TOP:
                        pageNumber = 0;
                        doGetMyMainPage(true, SwipyRefreshLayoutDirection.TOP);
                        break;

                    case BOTTOM:
                        pageNumber++;
                        doGetMyMainPage(false, SwipyRefreshLayoutDirection.BOTTOM);
                        break;
                    default:
                        break;
                }
            }
        });
        adapter = new CommunityPostMyMainPageAdapter(this);
        communityPostMyMainPageListview.setAdapter(adapter);
        communityPostMyMainPageListview.setOnItemClickListener(this);
    }

    private void doGetMyMainPage(boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        presenter.doGetMyMainPage(userid, pageNumber, isRefresh, direction);
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        assert getSupportActionBar() != null;
        if (!AsStrUtils.isEmpty(userid)) {
            if (userid.equals(MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USERID))) {
                getSupportActionBar().setTitle("我的帖子");
            } else {
                if (!AsStrUtils.isEmpty(username)) {
                    getSupportActionBar().setTitle(username + "的帖子");
                } else {
                    getSupportActionBar().setTitle("他的帖子");
                }
            }
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            userid = intent.getStringExtra("userid");
            username = intent.getStringExtra("username");
        }
    }

    @Override
    public void setDirection(SwipyRefreshLayoutDirection direction) {
        communityPostMyMainPageMPullRefreshView.setDirection(direction);
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        communityPostMyMainPageMPullRefreshView.setRefreshing(refreshing);
    }

    @Override
    public void loadData(boolean isRefresh, List<TopicData> topicList) {
        if (isRefresh) {
            adapter.clear();
        }
        adapter.appendToList(topicList);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TopicData topicItem = (TopicData) parent.getAdapter().getItem(position);
        String filepath = topicItem.getFilepath();
        Intent intent = new Intent(this, CommunityPostDetailActivity.class);
        if (topicItem.getTopicid() != null) {
            intent.putExtra("topicid", topicItem.getTopicid());
        }
        if (filepath != null) {
            String url = filepath + "?userid=" + MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USERID) + "&platform=ANDROID" + "&version=" + MyApplication.versionCode + "&udid=" + MyApplication.deviceId + "&babyid=" + MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_BABYID) + "&token=" + MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USER_TOKEN);
            intent.putExtra("url", url);
        }
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        presenter.detachView();
        super.onDestroy();
    }
}
